package com.asa.common.utils;

import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 
 * 利用ThreadLocal创建独立的实例,由 @see com.asa.common.tools.ComFilter 负责创建
 * 
 * 
 * 封装了 HttpServletRequest,HttpServletResponse
 * 
 * 
 * 如有文件上传(必须post请求)时多封装 MultipartHttpServletRequest
 * 
 * 
 * @author Asa 创建时间: 2015年11月8日 下午5:43:58
 * @version v1.0
 */

public class Com {
	private HttpServletRequest request;
	private HttpServletResponse response;
	private MultipartHttpServletRequest Mrequest;

	private static final String USER_KEY = "sessionuserKey";
	/**
	 * 设置 request 对象
	 * 
	 * @param request
	 */
	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public MultipartHttpServletRequest getMrequest() {
		return Mrequest;
	}

	public void setMrequest(MultipartHttpServletRequest mrequest) {
		Mrequest = mrequest;
	}

	/**
	 * 设置 response 对象
	 * 
	 * @param response
	 */
	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	/**
	 *  获得 request 对象 
	 * @return HttpServletRequest
	 */
	public HttpServletRequest getRequest() {
		return request;
	}

	/**
	 * 获得 response 对象
	 * 
	 * @return HttpServletResponse
	 */
	public HttpServletResponse getResponse() {
		return response;
	}

	/**
	 * 获得 ApplicationContext 对象
	 * 
	 * @return ApplicationContext
	 */
	public WebApplicationContext getApplicationContext() {
		if (request != null) {
			return WebApplicationContextUtils.getWebApplicationContext(getServletContext());
		} else {
			return null;
		}
	}

	/**
	 * 
	 * 获取项目的根路径
	 * 
	 * 
	 * @return String 项目的根路径
	 */
	public String getRealPath() {
		return request.getServletContext().getRealPath("/");
	}

	/**
	 * 获取Servlet的上下文
	 * 
	 * @return ServletContext
	 */
	public ServletContext getServletContext() {
		return request.getServletContext();
	}
	/**
	 * 获取当前session,如果不存在创建新的并返回
	 * @return  HttpSession
	 */
	public HttpSession getSession() {
		return request.getSession();
	}

	/**
	 * 获得访问用户的IP
	 * @return String 用户的IP
	 */
//	public String getIp() {
//		return HttpUtil.getClientIP(request);
//	}

	/**
	 *  获取前续页面的URL,如果没有则返回空字符串 
	 * @return String URL
	 */
	public String getReturnUrl() {
		String referer = request.getHeader("REFERER");
		if (referer == null)
			return "";
		return referer;
	}
	

	
	/**
	 * 在session中保存t数据,一般为用户数据,成为登录状态 
	 * @param t 记录的对象
	 */
//	public void setSessionUser(T t) {
//		getSession().setAttribute(USER_KEY, t);
//	}
	/**
	 * 获取session中的T ,一般为用户对象,如果没有则返回null 
	 * @return T 记录的对象
	 */
	@SuppressWarnings("unchecked")
//	public T getSessionUser() {
//		return (T) getSession().getAttribute(USER_KEY);
//	}

	/**
	 * 销毁session
	 */
	public void invalidateSession() {
		request.getSession().invalidate();
	}

	/**
	 * 将指定内容写入到响应流中
	 * @param content 内容
	 * @param type 类型 text/html,
	 */
	public void write(String content, String type) {
		try {
			/*response.setContentType(type + ";charset=UTF-8");
			response.setHeader("Pragma", "No-cache");
			response.setHeader("Cache-Control", "no-cache");
			response.setDateHeader("Expires", 0);*/
			response.getWriter().write(content);
			response.getWriter().flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 将指定对象转为JSON字符串，并写入到响应流中
	 * @param object 输出的内容
	 */
	public void write(Object object){
		write(JSON.toJSONString(object), "text/html");
	}
	
	/**
	 * 判断请求是否为异步请求 
	 * @return boolean
	 */
	public boolean isAjax() {
		return "XMLHttpRequest".equals(request.getHeader("X-Requested-With"));
	}
	/**
	 * 转发
	 * @param url
	 * @return 
	 */
	public String forward(String url){
		return "forward:"+url;
	}
	public String redirect(String url){
		return "redirect:"+url;
	}
	
	/**
	 * 判断请求是否POST请求
	 * 
	 * @return
	 */
	public boolean isPost() {
		return request.getMethod().equalsIgnoreCase("POST");
	}
	/** 
	 * attr:RESQUST中存入数据. <br> 
	 * @author asa 
	 * @param key
	 * @param o 
	 */  
	public void attr(String key,Object o){
		attr(key,o,Scope.RESQUST);
	}
	/** 
	 * attr:存入数据. <br> 
	 * @author asa 
	 * @param key
	 * @param o
	 * @param s Scope
	 */  
	public void attr(String key,Object o,Scope s){
		switch (s) {
		case RESQUST:
			request.setAttribute(key, o);
			break;
		case SESSION:
			getSession().setAttribute(key, o);
			break;
		default:
			getServletContext().setAttribute(key, o);
			break;
		}
	}
	
	public String para(String key){
		return request.getParameter(key);
	}
	/** 
	 * attr:RESQUST中获取数据. <br> 
	 * @author asa 
	 * @param key
	 * @return 
	 */  
	public String attr(String key){
		return (String)attr(key,Scope.RESQUST);
	}
	/** 
	 * attr:获取数据. <br> 
	 * @author asa 
	 * @param key
	 * @param s Scope
	 * @return 
	 */  
	public Object attr(String key,Scope s){
		switch (s) {
		case RESQUST:
			return request.getParameter(key);
		case SESSION:
			return getSession().getAttribute(key);
		default:
			return getServletContext().getAttribute(key);
		}
	}
	/** 
	 * removeAttr:RESQUST 中删除数据. <br> 
	 * @author asa 
	 * @param key 
	 */  
	public void removeAttr(String key){
		removeAttr(key,Scope.RESQUST);
	}
	/** 
	 * removeAttr:删除数据. <br> 
	 * @author asa 
	 * @param key
	 * @param s Scope
	 */  
	public void removeAttr(String key,Scope s){
		switch (s) {
		case RESQUST:
			 request.removeAttribute(key);
			 break;
		case SESSION:
			 getSession().removeAttribute(key);
			 break;
		default:
			getServletContext().removeAttribute(key);
			break;
		}
	}
	
}
